import * as dd from 'dingtalk-jsapi';


// 分享
function share(url, title, content, image) {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.util.share({
        type: 0, //分享类型，0:全部组件 默认； 1:只能分享到钉钉；2:不能分享，只有刷新按钮
        url: url,
        title: title,
        content: content,
        image: image,
        onSuccess: function () {
          resolve("ok")
          console.log('1111');

        },
        onFail: function (err) {
          console.log(err);
          console.log('221');
          resolve("err")
        }
      });
    });
  });
}
// 复制
function setData(text) {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.clipboardData.setData({
        text: text, //要复制粘贴板的内容
        onSuccess: function (result) {
          resolve("复制成功")
        },
        onFail: function () {
          resolve("复制失败")
        }
      });
    });
  });
}


function setRight(show, control, text) {
  return new Promise((resolve) => {
    dd.biz.navigation.setRight({
      show: show || false, //控制按钮显示， true 显示， false 隐藏， 默认true
      control: control || false, //是否控制点击事件，true 控制，false 不控制， 默认false
      text: text || "", //控制显示文本，空字符串表示显示默认文本
      onSuccess: function (result) {
        resolve("ok");
      },
      onFail: function (err) {
        resolve("false");
      }
    });

  });
}





function datepicker(value) {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.util.datepicker({
        format: 'yyyy/MM/dd', //注意：format只支持android系统规范，即2015-03-31格式为yyyy-MM-dd
        value: value || '', //默认显示日期
        onSuccess: function (result) {
          resolve(result.value);
        },
        onFail: function (err) {
          resolve(false);
        }
      })
    });
  });
}



function chooseInterval(defaultStart, defaultEnd) {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.calendar.chooseInterval({
        defaultStart: defaultStart || '',
        defaultEnd: defaultEnd || '',
        onSuccess: function (result) {
          resolve(result.value);
        },
        onFail: function (err) {
          resolve(false);
          console.log(err);

        }
      })
    });
  });
}

function uploadImageFromCamera() {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.util.uploadImageFromCamera({
        compression: true,
        onSuccess: ([imgURL]) => {
          resolve(imgURL)
        },
        onFail: error => {
          resolve(false);
        }
      });
    });
  });
}


// 检测钉钉版本
function watchDDFersion() {
  return new Promise((resolve) => {
    let isLow = false; //是否是低版本
    let version = dd.version; //version即容器版本号
    if (
      version.split(".")[0] < 4 ||
      (version.split(".")[0] == 4 && version.split(".")[1] < 6) ||
      (version.split(".")[0] == 4 && version.split(".")[1] == 6 && version.split(".")[2] < 39)
    ) {
      isLow = true;
    }
    resolve(isLow);
  });
}


// 检测钉钉版本是否高于4.7.22
function watchDDFersion2() {
  return new Promise((resolve) => {
    let isLow = false; //是否是低版本
    let version = dd.version; //version即容器版本号
    if (
      version.split(".")[0] < 4 ||
      (version.split(".")[0] == 4 && version.split(".")[1] < 7) ||
      (version.split(".")[0] == 4 && version.split(".")[1] == 7 && version.split(".")[2] < 22)
    ) {
      isLow = true;
    }
    resolve(isLow);
  });
}


function openLink(url) {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.util.openLink({
        url: url,
        onSuccess: function (result) {
          resolve("ok")
        },
        onFail: function (err) {
          resolve("err")
        }
      });
    });
  });
}

function setScreenBrightnessAndKeepOn(brightness = 1, isKeep = true) {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.biz.util.setScreenBrightnessAndKeepOn({
        brightness: brightness,
        isKeep: isKeep,
        onSuccess: function (result) {
          resolve("ok")
        },
        onFail: function (error) {
          resolve("err")
        }
      });
    });
  });
}

export default {
  share,
  setData,
  setRight,
  datepicker,
  chooseInterval,
  uploadImageFromCamera,
  watchDDFersion,
  watchDDFersion2,
  openLink,
  setScreenBrightnessAndKeepOn
}
